package com.zfc.threadpool.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author zfc
 * @version 1.0
 */

@Getter
@AllArgsConstructor
public enum RejectedPolicyType {
    ABORT("AbortPolicy", new ThreadPoolExecutor.AbortPolicy()),
    DISCARD("DiscardPolicy", new ThreadPoolExecutor.DiscardPolicy()),
    DISCARD_OLDEST("DiscardOldestPolicy", new ThreadPoolExecutor.DiscardOldestPolicy()),
    CALLER_RUNS("CallerRunsPolicy", new ThreadPoolExecutor.CallerRunsPolicy()),
    ;

    private final String type;
    private final RejectedExecutionHandler handler;

    public static RejectedPolicyType of(String type) {
        return Arrays.stream(values())
            .filter(policy -> policy.getType().equalsIgnoreCase(type))
            .findFirst()
            .orElse(ABORT); // 默认AbortPolicy
    }
}

